package top.ochiamalu.aioj.strategy.thumb.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Component;
import top.ochiamalu.aioj.domain.entity.Question;
import top.ochiamalu.aioj.domain.entity.Thumb;
import top.ochiamalu.aioj.enums.ThumbTypeEnum;
import top.ochiamalu.aioj.service.QuestionService;
import top.ochiamalu.aioj.strategy.thumb.ThumbStrategy;

/**
 * 题目点赞策略
 *
 * @author ochiamalu
 * @date 2025/03/07
 */
@Component
public class QuestionThumbStrategy implements ThumbStrategy {
    
    private final QuestionService questionService;

    public QuestionThumbStrategy(QuestionService questionService) {
        this.questionService = questionService;
    }

    @Override
    public void buildQueryCondition(LambdaQueryWrapper<Thumb> queryWrapper, Long targetId) {
        queryWrapper.eq(Thumb::getTargetId, targetId)
                   .eq(Thumb::getType, ThumbTypeEnum.QUESTION.getValue());
    }

    @Override
    public void handleAfterThumb(Long targetId, boolean isAdd) {
        Question question = questionService.getById(targetId);
        if (question != null) {
            // 更新点赞数
            Question updateQuestion = new Question();
            updateQuestion.setId(targetId);
            updateQuestion.setThumbNum(question.getThumbNum() + (isAdd ? 1 : -1));
            questionService.updateById(updateQuestion);
        }
    }
} 